package com.example.gcode;

import com.utils.Constants;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import com.gcloud.*;

public class AdminActivity extends Activity {

	private final String TAG = AdminActivity.class.getSimpleName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adminlayout);
		TabHost th = (TabHost) findViewById(R.id.tabhost);
		// Button newTab=(Button) findViewById(R.id.baddTab);
		th.setup();
		TabSpec specs = th.newTabSpec("tag1");
		specs.setContent(R.id.tab1);
		specs.setIndicator("New Account");
		th.addTab(specs);
		TabSpec specs2 = th.newTabSpec("tag2");
		specs2.setContent(R.id.tab2);
		specs2.setIndicator("My Accounts");
		th.addTab(specs2);
		TabSpec specs3 = th.newTabSpec("tag3");
		specs3.setContent(R.id.tab3);
		specs3.setIndicator("Settings");
		th.addTab(specs3);

		ListView listView = (ListView) findViewById(R.id.listViewAccounts);
		BaseAdapter baseAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, getUserActiveAccounts());
		listView.setAdapter(baseAdapter);
		registerForContextMenu(listView);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent fileOptionsIntent = new Intent(AdminActivity.this,
						FileOptionsActivity.class);
				String item = ((TextView) view).getText().toString();
				if (item.equals(Constants.googleDriveTitle)) {
					fileOptionsIntent.putExtra(Constants.target,
							com.enums.TargetApi.GOOGLEDRIVE);
				} else if (item.equals(Constants.dropBoxTitle)) {
					fileOptionsIntent.putExtra(Constants.target,
							com.enums.TargetApi.DROPBOX);
				} else if (item.equals(Constants.skyDriveTitle)) {
					fileOptionsIntent.putExtra(Constants.target,
							com.enums.TargetApi.SKYDRIVE);
				} else if (item.equals(Constants.ubuntuOneTitle)) {
					fileOptionsIntent.putExtra(Constants.target,
							com.enums.TargetApi.UBUNTUONE);
				}
				startActivity(fileOptionsIntent);
			}

		});
	}

	private String[] getUserActiveAccounts() {
		// TODO: this method will get the active accounts from users account
		// database
		String methodName = "getUserActiveAccounts()";
		Log.i(TAG, methodName);

		String userActiveAccounts[] = new String[] { "Google Drive", "DropBox",
				"SkyDrive", "Ubuntu One" };
		return userActiveAccounts;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		String methodName = "onCreateContextMenu() ";
		Log.i(TAG, methodName + "click here added");
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add("Click here");

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		String methodName = "onContextItemSelected()";
		Log.i(TAG, methodName);
		if (item.getTitle() == "Click here") {
			Toast.makeText(this, "Google drive selected", Toast.LENGTH_LONG)
					.show();
		} else if (item.getTitle() == "DropBox") {
			Toast.makeText(this, "DropBox selected", Toast.LENGTH_LONG).show();
		} else if (item.getTitle() == "DropBox") {
			Toast.makeText(this, "SkyDrive selected", Toast.LENGTH_LONG).show();
		} else if (item.getTitle() == "Ubuntu One") {
			Toast.makeText(this, "Ubuntu one selected", Toast.LENGTH_LONG)
					.show();
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
